package com.project.controller;

import com.project.bean.CutPageBean;
import com.project.bean.StudentBean;
import com.project.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("stu")
public class StudentController {

    @Autowired
    private StudentService service;

    @RequestMapping("findByItem")
    public CutPageBean<StudentBean> findByItem(int pageNo, String name, String className, String roomAddress) {
        CutPageBean<StudentBean> byItem = service.findByItem(pageNo, name, className, roomAddress);
        return byItem;
    }

    @RequestMapping("del")
    public void delete(int id) {
        service.delStu(id);
    }

    @RequestMapping("changeRoom")
    public void changeRoom(int studentId, int roomId) {
        service.updateRoom(studentId, roomId);
    }

    @RequestMapping("findById")
    public StudentBean findById(int id) {
        StudentBean studentBean = service.findById(id);
//        System.out.println(studentBean.getRoomBean().getAddress());
        return service.findById(id);
    }

    @RequestMapping("add")
    public void add(String name, String sex, Integer room, Integer className, @RequestParam("face") MultipartFile mf, String phone)throws IOException {
        String faceImg = mf.getOriginalFilename();
        StudentBean studentBean = new StudentBean(name, sex, phone, className, room, faceImg);
        service.add(studentBean);
        mf.transferTo(new File("/Users/cengyunke/Desktop/java/studentRoom/src/main/resources/static/studentFace/" + faceImg));
    }
}
